package com.jic.manager.controller.manage.conf;

import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.entity.SysNotice;
import com.jic.conf.request.SysNoticeRequest;
import com.jic.conf.response.SysNoticeResponse;
import com.jic.manager.enums.ResponseCode;
import com.jic.manager.exception.AppRuntimeException;
import com.jic.manager.service.SysNoticeService;
import com.jic.manager.utils.BeanUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @author: MJ
 * @date: 2020-01-09
 * @remark: 平台公告后台管理系统的服务
 */
@Slf4j
@RestController
@RequestMapping("/sysNotice")
public class SysNoticeController {

    @Autowired
    SysNoticeService sysNoticeService;


    /*
    * 分页查询
    * */
    @ApiOperation(value = "获取所有平台公告")
    @ResponseBody
    @RequestMapping("/queryList")
    public RestResult<PageResult<SysNoticeResponse>> queryList(@RequestBody SysNoticeRequest form) {
        PageResult<SysNotice> result = sysNoticeService.queryPage(form);
        List<SysNoticeResponse> sysNoticeResponseList = result.getRows().stream().map(temp -> {
            SysNoticeResponse sysNoticeResponse = BeanUtil.copy(temp, SysNoticeResponse.class);
            return sysNoticeResponse;
        }).filter(item -> item != null).collect(Collectors.toList());
        PageResult<SysNoticeResponse> pageResult = new PageResult<>();
        pageResult.setRows(sysNoticeResponseList);
        pageResult.setTotal(result.getTotal());
        return RestResult.success(pageResult);
    }



    /*
    * 新增/修改平台公告
    * */
    @ApiOperation(value = "新增/修改平台公告")
    @ResponseBody
    @RequestMapping("/editSysNotice")
    public RestResult<String> editSysNotice(@RequestBody SysNoticeRequest form) {
        RestResult restResult = new RestResult<>();
        try {
            SysNoticeRequest sysNoticeRequest = BeanUtil.copy(form, SysNoticeRequest.class);
            //修改
            if(null!=sysNoticeRequest.getId()&&!"".equals (sysNoticeRequest.getId())){
                long flag=sysNoticeService.update(sysNoticeRequest);
                restResult=RestResult.success(restResult);
            }else {
                long flag=sysNoticeService.insert(sysNoticeRequest);
                restResult=RestResult.success(restResult);
            }

        } catch (Exception e) {
            log.error("异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return restResult;
    }


    /*
     * 删除平台公告
     * */
    @ApiOperation(value = "删除平台公告")
    @ResponseBody
    @RequestMapping("/deleteInfo")
    public RestResult<String> deleteInfo( @RequestBody SysNoticeRequest request) {
        RestResult restResult = new RestResult<>();
        try {
                sysNoticeService.delete(request.getId());

        } catch (Exception e) {
            log.error("异常",e);
            throw new AppRuntimeException(ResponseCode.EXCEPTION);
        }
        return RestResult.success("成功");
    }

     /*
     * 审核
     * */
    @ApiOperation(value = "审核")
    @ResponseBody
    @RequestMapping("/setAudit")
    public RestResult<Integer> setAudit(@RequestBody SysNoticeRequest request) {
        return RestResult.success(sysNoticeService.setaudit(request));
    }


}
